<template>
  <s-modal
    title="修改"
    :visible="visible"
    width="800px"
    ok-text="保存"
    :confirm-loading="confirmLoading"
    :closable="!confirmLoading"
    @ok="save()"
    @cancel="
      () => {
        if (!confirmLoading) {
          visible = false
        }
      }
    "
  >
    <div style="height: 400px">
      <s-scroll>
        <s-form ref="form" value="formValue">
          <s-form-item
            label="字典文本"
            name="text"
            :rules="[
              { whitespace: true, required: true, message: '字典文本不能为空' },
              { max: 50, message: '最长为50' }
            ]"
          >
            <s-input v-model="formValue.text" allow-clear placeholder="字典文本" />
          </s-form-item>
          <s-form-item
            label="字典值"
            name="value"
            :rules="[
              { whitespace: true, required: true, message: '字典值不能为空' },
              { max: 50, message: '最长为50' }
            ]"
          >
            <s-input v-model="formValue.value" allow-clear placeholder="字典值" />
          </s-form-item>
          <s-form-item
            label="排序号"
            name="orderNum"
            :rules="[{ required: true, message: '排序号不能为空' }]"
          >
            <s-input-number v-model="formValue.orderNum" :min="1" :max="999" placeholder="排序号" />
          </s-form-item>
          <s-form-item
            label="备注"
            name="remark"
            size="big"
            :rules="[{ max: 200, message: '最长为200' }]"
          >
            <s-text-area v-model="formValue.remark" allow-clear placeholder="字典备注" />
          </s-form-item>
        </s-form>
      </s-scroll>
    </div>
  </s-modal>
</template>

<script>
export default {
  name: 'Update',
  names: {
    success: {
      type: Function,
      required: false,
      default: () => {}
    }
  },
  data() {
    return {
      formValue: {},
      confirmLoading: false,
      visible: false
    }
  },
  methods: {
    open(id, groupId) {
      this.groupId = groupId
      this.$loading(true)
      this.$api
        .GET('/data/dict/item/selectOneById', { id: id })
        .then((res) => {
          if (!res.data) {
            this.$loading(false)
            this.visible = false
            this.$message.warn('数据不存在')
          } else {
            this.formValue = res.data
            this.$loading(false)
            this.visible = true
          }
        })
        .catch(() => {
          this.$loading(false)
        })
    },
    save() {
      const form = this.$refs.form
      form.validate((values) => {
        this.confirmLoading = true
        values.id = this.formValue.id
        values.groupId = this.groupId
        this.$api
          .PUT('/data/dict/item/update', values)
          .then((res) => {
            this.confirmLoading = false
            this.visible = false
            this.$emit('success')
            this.$message.info(res.msg)
          })
          .catch(() => {
            this.confirmLoading = false
          })
      })
    }
  }
}
</script>

<style lang="less" scoped></style>
